package com.tx.studentManager.controller;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tx.common.format.util.DateUtil;
import com.tx.studentManager.config.SystemConfig;
import com.tx.studentManager.model.Department;
import com.tx.studentManager.model.Major;
import com.tx.studentManager.model.Page;
import com.tx.studentManager.qo.MajorQO;
import com.tx.studentManager.service.DepartmentService;
import com.tx.studentManager.service.MajorService;
import com.tx.studentManager.utils.DWZJsonUtil;

/**
 * 
 * @描述 专业操作控制器
 * @since studentManager-admin 1.0
 * @version 1.0 2015-9-14 下午3:07:26
 * @author 此生唯晴
 */
@Controller
@RequestMapping(value = "/major")
public class MajorController {
	private Logger logger=Logger.getLogger(MajorController.class);
	@Autowired
	private MajorService majorService;
	@Autowired
	private DepartmentService depService;
	@Autowired
	private SystemConfig systemConfig;

	/**
	 * 
	 * @描述 查询专业
	 * @param qo
	 * @param createDateStartStr
	 * @param createDateEndStr
	 * @param pageSize
	 * @param pageNo
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(
			@ModelAttribute MajorQO qo,
			@RequestParam(required = false) String createDateStartStr,
			@RequestParam(required = false) String createDateEndStr,
			@RequestParam(required = false, value = "numPerPage") Integer pageSize,
			@RequestParam(required = false, value = "pageNum") Integer pageNo) {

		if (StringUtils.isNotBlank(createDateEndStr)) {
			qo.setCreateDateEnd(DateUtil.str2DateTime(createDateEndStr));
		}
		if (StringUtils.isNotBlank(createDateStartStr)) {
			qo.setCreateDateStart(DateUtil.str2DateTime(createDateStartStr));
		}
		if (StringUtils.isBlank(qo.getDepId())) {
			qo.setDepId(null);
		}
		ModelAndView modelAndView = new ModelAndView("major/majorList");
		// 默认查询第一页，每页10条数据
		pageSize = pageSize == null ? systemConfig.getQueryPageSize() : pageSize;
		pageNo = pageNo == null ? 1 : pageNo;

		Page page = new Page();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page.setCondition(qo);
		page = majorService.list(page);
		List<Department> depList = depService.findAll();
		modelAndView.addObject("depList", depList);
		modelAndView.addObject("page", page);
		return modelAndView;
	}

	/**
	 * 
	 * @描述 添加专业，返回视图
	 * @return
	 */
	@RequestMapping(value = "/add")
	public ModelAndView add() {
		ModelAndView modelAndView = new ModelAndView("major/majorAdd");
		List<Department> depList = depService.findAll();
		modelAndView.addObject("depList", depList);
		return modelAndView;
	}

	/**
	 * @描述 添加专业，持久化
	 * @param majorName
	 * @param depId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/toAdd", produces = "text/html;charset=UTF-8")
	public String toAdd(@RequestParam(required = true) String majorName,
			@RequestParam(required = true) String depId) {
		int sameMajorNum = majorService.findBy("majorName", majorName).size();
		if (sameMajorNum > 0) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE,
					"该专业已存在！", null, null);
		}
		Department selectDep = depService.findById(depId);
		if (selectDep == null) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE,
					"所选院系不存在！", null, null);
		}
		Major major = new Major();
		major.setMajorName(majorName);
		major.setDepartment(selectDep);
		major = majorService.insert(major);
		if (major == null) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE,
					"操作成功", null, null);
		}
		return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE,
				"添加成功", DWZJsonUtil.OPER_CLOSE_CURRENT, null);
	}

	/**
	 * 
	 * @描述 删除专业
	 * @param majorId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delMajor", produces = "text/html;charset=UTF-8")
	public String delMajor(String majorId) {
		// 先检查改专业是否存在
		Major checkMajor = majorService.load(majorId);
		if (checkMajor == null) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE,
					"所选专业不存在", null, null);
		}
		boolean delResult = false;
		try {
			delResult = majorService.delete(majorId);
		} catch (DataIntegrityViolationException e) {
			String errorMsg="存在关联班级，不允许删除该专业！！！";
			logger.info(errorMsg);
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE,
					errorMsg, null, null);
		}
		if (delResult) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE,
					"删除成功", null, null);
		} else {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE,
					"删除失败", null, null);
		}
	}

	@RequestMapping("/modify")
	public ModelAndView modity(String majorId) {
		Major major = majorService.findById(majorId);
		ModelAndView modelAndView = new ModelAndView("major/majorModify");
		List<Department> depList = depService.findAll();
		modelAndView.addObject(major);
		modelAndView.addObject("depList",depList);
		return modelAndView;
	}

	@ResponseBody
	@RequestMapping(value = "/update", produces = "text/html;charset=UTF-8")
	public String update(Major major,String depId) {
		Major loadMajor=majorService.findById(major.getMajorId());
		if (StringUtils.isNotBlank(major.getMajorName())) {
			loadMajor.setMajorName(major.getMajorName());
		}
		if (StringUtils.isNotBlank(depId)) {
			loadMajor.setDepartment(depService.findById(depId));
		}
		if (majorService.update(loadMajor)) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE,
					"更新成功", DWZJsonUtil.OPER_CLOSE_CURRENT, null);
		} else {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE,
					"更新失败", DWZJsonUtil.OPER_CLOSE_CURRENT, null);
		}
	}
}
